<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MongoDB 副本集搭建 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MongoDB 副本集搭建</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<h2 id="MongoDB-副本集（repl-set-介绍"><a href="#MongoDB-副本集（repl-set-介绍" class="headerlink" title=".MongoDB 副本集（repl set)介绍"></a>.MongoDB 副本集（repl set)介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;早期版本使用master-slave，一主一从和MySQL类似，但slave在此架构中为只读，当主库宕机后，从库不能自动切换为主</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目前已经淘汰master-slave模式，改为副本集，这种模式下有一个主(primary)，和多个从(secondary)，只读。支持给它们设置权重，当主宕掉后，权重最高的从切换为主</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在此架构中还可以建立一个仲裁(arbiter)的角色，它只负责裁决，而不存储数据</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再此架构中读写数据都是在主上，要想实现负载均衡的目的需要手动指定读库的目标server</p>
<h2 id="2-副本集架构图"><a href="#2-副本集架构图" class="headerlink" title="2.副本集架构图"></a>2.副本集架构图</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<h2 id="3-MongoDB副本集搭建"><a href="#3-MongoDB副本集搭建" class="headerlink" title="3.MongoDB副本集搭建"></a>3.MongoDB副本集搭建</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;三台机器： 192.168.0.77（primary）   192.168.0.76（secondary)    192.168.0.75（secondary)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑三台机器的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># vim /etc/mongod.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">replication:</div><div class="line"><span class="comment">##oplog大小</span></div><div class="line">  oplogSizeMB: 20</div><div class="line"><span class="comment">##复制集名称</span></div><div class="line">  replSetName: yanyi</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别重启三台机器</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连接主，在主上运行命令mongo</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&gt; use admin</div><div class="line">&gt; config=&#123;_id:<span class="string">"yanyi"</span>,members:[&#123;_id:0,host:<span class="string">"192.168.0.77:27017"</span>&#125;,&#123;_id:1,host:<span class="string">"192.168.0.76:27017"</span>&#125;,&#123;_id:2,host:<span class="string">"192.168.0.75:27017"</span>&#125;]&#125;</div><div class="line">&gt; rs.initiate(config)</div><div class="line">&gt; rs.add(<span class="string">"192.168.0.76"</span>)</div><div class="line">&gt; rs.add(<span class="string">"192.168.0.75"</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rs.status() //查看状态</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果两个从上的状态为”stateStr” : “STARTUP”, 则需要进行如下操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&gt; var config=&#123;_id:<span class="string">"yanyi"</span>,members:[&#123;_id:0,host:<span class="string">"192.168.0.77:27017"</span>&#125;,&#123;_id:1,host:<span class="string">"192.168.0.76:27017"</span>&#125;,&#123;_id:2,host:<span class="string">"192.168.0.75:27017"</span>&#125;]&#125;</div><div class="line">&gt;rs.reconfig(config)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时再次查看rs.status()会发现从的状态变为SECONDARY</p>
<h2 id="4-MongoDB-副本集测试"><a href="#4-MongoDB-副本集测试" class="headerlink" title="4.MongoDB 副本集测试"></a>4.MongoDB 副本集测试</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上建库，建集合</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">yanyi:PRIMARY&gt; use mydb</div><div class="line">yanyi:PRIMARY&gt; db.acc.insert(&#123;AccountID:1,UserName:<span class="string">"123"</span>,password:<span class="string">"123456"</span>&#125;)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yanyi:PRIMARY&gt; show dbs</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若出现错误Error: listDatabases failed:{ “note” : “from execCommand”, “ok” : 0, “errmsg” : “not master” }，需要执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yanyi:SECONDARY&gt; rs.slaveOk()</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<h2 id="5-MongoDB副本集更改权重模拟主宕机"><a href="#5-MongoDB副本集更改权重模拟主宕机" class="headerlink" title="5.MongoDB副本集更改权重模拟主宕机"></a>5.MongoDB副本集更改权重模拟主宕机</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认三台机器权重都为1，如果任何一个权重设置为比其他的高，则该台机器马上切换为primary角色，所以我们预设三台机器的权重分别为：77:3,76:2,75:1</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在主上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">yanyi:PRIMARY&gt; cfg = rs.conf()</div><div class="line">yanyi:PRIMARY&gt; cfg.members[0].priority = 3</div><div class="line">yanyi:PRIMARY&gt; cfg.members[1].priority = 2</div><div class="line">yanyi:PRIMARY&gt; cfg.members[2].priority = 1</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yanyi:PRIMARY&gt; rs.reconfig(cfg)</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%89%AF%E6%9C%AC%E9%9B%86%E6%90%AD%E5%BB%BA/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样的话，第二个节点将会成为候选主节点。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上执行 iptables -I INPUT -p tcp –dport 27017 -j DROP</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@primary ~]<span class="comment"># iptables -I INPUT -p tcp --dport 27017 -j DROP</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/4. MongoDB 副本集搭建/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
